﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hongapo_Common;
using System.Text;
public partial class sysadmin_Mall_malladd : PageBase
{
    Hongapo_Model.Mall.Mall model = new Hongapo_Model.Mall.Mall();
    Hongapo_Services.Mall.Mall bll = new Hongapo_Services.Mall.Mall();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadMallType();
            if (MallID != 0)
            {
                LoadText();
            }
            ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
        }
    }
    public int MallID
    {

        get
        {
            return TypeConvert.ToInt32(Request["MallID"], 0);
        }
    }

    #region 加载商城类别
    private void LoadMallType()
    {
        DataTable dt = new Hongapo_Services.Mall.MallType().GetList(" 1=1 order by sort").Tables[0];
        StringBuilder str = new StringBuilder();
        string types = "";
        model = bll.GetModel(MallID);
        if (model != null)
        {
            types = model.MallType;
        }

        foreach (DataRow dr in dt.Rows)
        {
            if (types != "")
            {
                if (types.IndexOf(dr["ID"].ToString()) != -1)
                {
                    str.Append("<li>" + dr["Name"] + "：<input type=\"checkbox\" value=\"" + dr["ID"] + "\" name=\"Malltype\" checked=\"true\"> </li>");
                }
                else
                {
                    str.Append("<li>" + dr["Name"] + "：<input type=\"checkbox\" value=\"" + dr["ID"] + "\" name=\"Malltype\"> </li>");
                }
            }
            else
            {
                str.Append("<li>" + dr["Name"] + "：<input type=\"checkbox\" value=\"" + dr["ID"] + "\" name=\"Malltype\"> </li>");
            }

        }
        this.ltlType.Text = str.ToString();
    }
    #endregion

    #region 新增或修改
    private void Add()
    {
        model.MallName = this.txtName.Text.Trim();
        if (!string.IsNullOrEmpty(model.MallName))
        {
            model.Logo = "";
            model.Clicks = TypeConvert.ToInt32(this.txtClick.Text.Trim());
            model.Criticis = TypeConvert.ToInt32(this.txtCriticis.Text.Trim());
            model.IsAudit = this.chkIsAudit.Checked == true ? 1 : 0;
            model.Isopen = this.chkIsAudit.Checked == true ? 1 : 0;
            model.Location = this.txtAddress.Text;
            model.MallDesc = this.txtdesc.Text;
            model.MallInfo = this.txtInfo.Value;
            model.MallNo =MallID!=0?this.hiddenMallNo.Value:DateTime.Now.ToString("yy") + Hongapo_Services.Comon.GetNumber(1);
            model.MallType = this.hiddenIdlist.Value.Trim(',');
            model.Recomens = TypeConvert.ToInt32(this.txtRecoms.Text.Trim());
            model.Remark = "";
            model.Services = this.txtServices.Text;
            model.SiteTel = this.txtTel.Text;
            model.Sort = TypeConvert.ToInt32(this.txtSort.Text.Trim());
            model.StarLevel = TypeConvert.ToInt32(this.txtStart.Text.Trim());
            model.Title = this.txtTitle.Text;
            model.Url = this.txtURL.Text;
            model.CreateDate = DateTime.Now;
            model.CreateUser = UserID;
            try
            {
                if (!string.IsNullOrEmpty(this.fileUpload.FileName))
                {
                    string path = "~/images/mall/" + this.fileUpload.FileName + "";
                    this.fileUpload.PostedFile.SaveAs(MapPath(path));
                    model.Logo = this.fileUpload.FileName;
                }
                else
                {
                    model.Logo = this.hiddenLogo.Value;
                }
                if (MallID != 0)
                {
                    model.ID = MallID;
                    bll.Update(model);
                }
                else
                {
                    bll.Add(model);
                }
                this.Show("操作成功！", ViewState["UrlReferrer"] as string);

            }
            catch (Exception ex)
            {
                this.Show(ex.ToString());
            }
        }
        else
        {
            this.Show("请输入商城名称！");
        }
    }
    #endregion

    #region 更新时加载数据
    private void LoadText()
    {
        model = bll.GetModel(MallID);
        this.txtAddress.Text = model.Location;
        this.txtClick.Text = model.Clicks.ToString();
        this.txtCriticis.Text = model.Criticis.ToString();
        this.txtdesc.Text = model.MallDesc;
        this.txtInfo.Value = model.MallInfo;
        this.txtName.Text = model.MallName;
        this.txtRecoms.Text = model.Recomens.ToString();
        this.txtServices.Text = model.Services;
        this.txtSort.Text = model.Sort.ToString();
        this.txtStart.Text = model.StarLevel.ToString();
        this.txtTel.Text = model.SiteTel;
        this.txtTitle.Text = model.Title;
        this.txtURL.Text = model.Url;
        this.chkIsAudit.Checked = model.IsAudit == 1 ? true : false;
        this.chkIsopen.Checked = model.Isopen == 1 ? true : false;
        this.hiddenLogo.Value = model.Logo;
        this.hiddenMallNo.Value = model.MallNo;
    }
    #endregion

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Add();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["UrlReferrer"] as string);
    }
}
